﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class ScheduleManager_DeptSchedule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void imbtnSerch_Click(object sender, ImageClickEventArgs e)
    {
        string dept=this.txtDept.Text.Trim();
        string name=this.txtName.Text.Trim();
        DateTime date =Convert.ToDateTime(this.txtDate.Value.Trim());
       List<DateTime> beginandEnd= GetDay(date);
       this.GridView1.DataSource = new ScheduleManager().GetScheduleByDeptNameCreateTimeName(beginandEnd[0], beginandEnd[1], dept, name);
       this.GridView1.DataBind();
    }
    //protected void txtDate_TextChanged(object sender, EventArgs e)
    //{
    //    this.lbDate.Text = this.txtDate.Text;
    //}
    public int GetDays(DateTime date)
    {
        int day = date.Day;
        return day;
    }
    private List<DateTime> GetDay(DateTime date)
    {
        List<DateTime> be = new List<DateTime>();
        string week = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek);
        switch (week)
        {
            case "星期日": be.Insert(0,date);
                if (date.Day + 6 < 30)
                {
                    be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day + 6).ToString()));
                }
                else
                {
                   be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month + 1).ToString() + "-" + (date.Day + 6 - 30).ToString()));
                }

                break;
            case "星期一":
                if (date.Day - 1 > 0)
                {
                    be.Insert(0,Convert.ToDateTime(date.Year + "-" + date.Month + "-" + (date.Day - 1).ToString()));
                }
                else
                {
                   be.Insert(0, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month - 1).ToString() + "-" + (date.Day - 1 + 30).ToString()));
                }
                if (date.Day + 5 > 30)
                {
                    be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month + 1).ToString() + "-" + (date.Day + 5 - 30).ToString()));
                }
                else
                {
                    be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day + 5).ToString()));
                }

                break;
            case "星期二":
                if (date.Day - 2 > 0)
                {
                    be.Insert(0,Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day - 2).ToString()));
                }
                else
                {
                   be.Insert(0, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month - 1).ToString() + "-" + (date.Day - 2 + 30).ToString()));
                }
                if (date.Day + 4 > 30)
                {
                   be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month + 1).ToString() + "-" + (date.Day + 4 - 30).ToString()));
                }
                else
                {
                   be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day + 4).ToString()));
                }


                break;
            case "星期三":
                if (date.Day - 3 < 0)
                {
                   be.Insert(0, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month - 1).ToString() + "-" + (date.Day - 3 + 30).ToString()));
                }
                else
                {
                   be.Insert(0, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day - 3).ToString()));
                }
                if (date.Day + 3 > 30)
                {
                   be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month + 1).ToString() + "-" + (date.Day + 3 - 30).ToString()));
                }
                else
                {
                    be.Insert(1,Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day + 3).ToString()));
                }


                break;
            case "星期四":
                if (date.Day - 4 < 0)
                {
                    be.Insert(0,Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month - 1).ToString() + "-" + (date.Day - 4 + 30).ToString()));
                }
                else
                {
                    be.Insert(0, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day - 4).ToString()));
                }
                if (date.Day + 2 > 30)
                {
                   be.Insert(1,Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month + 1).ToString() + "-" + (date.Day + 2 - 30).ToString()));
                }
                else
                {
                    be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day + 2).ToString()));
                }

                break;
            case "星期五":
                if (date.Day - 5 < 0)
                {
                    be.Insert(0,Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month - 1).ToString() + "-" + (date.Day - 5 + 30).ToString()));
                }
                else
                {
                    be.Insert(0,Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day - 5).ToString()));
                }
                if (date.Day + 1 > 30)
                {
                    be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month + 1).ToString() + "-" + (date.Day + 1 - 30).ToString()));
                }
                else
                {
                   be.Insert(1, Convert.ToDateTime(date.Year.ToString() + "-" + date.Month.ToString() + "-" + (date.Day + 1).ToString()));
                }
                break;
            case "星期六":
                if (date.Day - 6 < 0)
                {
                   be.Insert(0, Convert.ToDateTime(date.Year.ToString() + "-" + (date.Month - 1).ToString() + "-" + (date.Day - 6 + 30).ToString()));
                }
                else
                {
                    be.Insert(0,  Convert.ToDateTime(date.Year + "-" + date.Month + "-" + (date.Day - 6).ToString()));
                }
                be.Insert(1, Convert.ToDateTime(date.Year + "-" + date.Month + "-" + (date.Day).ToString()));

                break;

        }
        return be;
    }
}